# 
# Project: Last.sk
# File: profile.py
# Author: Angel Luis Calvo Ortega
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import xml.sax.handler

# Modes
#  0: None
#  1: playcount
#  2: avatar
#  3: date
#  4: country
#  5: registered

HTML_CODE = '''
<b>Tracks played:</b> %s<br>
<b>Registered:</b> %s
'''

class ProfileHandler(xml.sax.handler.ContentHandler):

    def __init__(self):
        self.name = ''
        self.playcount = ''
        self.avatar = ''
        self.date = ''
        self.country = ''
        self.mode = 0
        self.cached = False
        self.url = 'http://ws.audioscrobbler.com/1.0/user/%s/profile.xml'
        
    def toString(self):
        return  HTML_CODE % (self.playcount, self.registered)
    
    #Sax Methods
    def startDocument(self):
        self.name = ''
        self.playcount = ''
        self.avatar = ''
        self.date = ''
        self.country = ''
        self.mode = 0
       
    def startElement(self, name, attributes):
        if name == 'profile':
            self.name = attributes['username']
        elif name == 'playcount':
            self.mode = 1
        elif name == 'avatar':
            self.mode = 2
        elif name == 'date':
            self.mode = 3
        elif name == 'country':
            self.mode = 4
        elif name == 'registered':
            self.mode = 5
                                        
    def characters(self, data):
        if self.mode == 1:
            self.playcount = data
        elif self.mode == 2:
            self.avatar = data
        elif self.mode == 3:
            self.date = data
        elif self.mode == 4:
            self.country = data
        elif self.mode == 5:
            self.registered = data
                            
    def endElement(self, name):
        self.mode = 0
